package vip.xiaonuo.modular.business.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.core.annotion.BusinessLog;
import vip.xiaonuo.core.enums.LogAnnotionOpTypeEnum;
import vip.xiaonuo.core.pojo.response.ResponseData;
import vip.xiaonuo.core.pojo.response.SuccessResponseData;
import vip.xiaonuo.modular.manage.poidata.param.PoiDataParam;
import vip.xiaonuo.modular.manage.poidata.service.PoiDataService;
import vip.xiaonuo.modular.manage.poidatatags.param.PoiDataTagsParam;
import vip.xiaonuo.modular.manage.poidatatags.service.PoiDataTagsService;
import vip.xiaonuo.sys.modular.area.entity.SysArea;
import vip.xiaonuo.sys.modular.area.param.SysAreaParam;
import vip.xiaonuo.sys.modular.area.service.SysAreaService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Description poi数据管理
 * @Author cy
 * @Date 2022/8/4 14:54
 * @Version 1.0
 */
@Api(tags = "poi数据")
@RestController
public class PoiDataApiController {

    @Resource
    private PoiDataService poiDataService;

    @Resource
    private PoiDataTagsService poiDataTagsService;

    @Resource
    private  SysAreaService sysAreaService;

    @ApiOperation("POI画像查询")
    @PostMapping("/business/biPoiData/queryImageType")
    public ResponseData queryImageType(@RequestBody PoiDataParam poiDataParam) {
        return new SuccessResponseData(poiDataService.queryImageType(poiDataParam));
    }

    @ApiOperation("POI地址查询")
    @PostMapping("/business/biPoiData/poiQuery")
    public ResponseData poiQuery(@RequestBody PoiDataParam poiDataParam) {
        return new SuccessResponseData(poiDataService.customList(poiDataParam));
    }
    @ApiOperation("POI类型树查询")
    @PostMapping("/business/biPoiData/poiTagTree")
    public ResponseData poiTagTree(@RequestBody PoiDataTagsParam poiDataTagsParam) {
        return new SuccessResponseData(poiDataTagsService.listTree(poiDataTagsParam));
    }

    /**
     * 系统区域列表（省）
     *
     * @author xuyuxiang
     * @date 2020/3/20 21:23
     */
    @GetMapping("/business/sysArea/listProvince")
    @BusinessLog(title = "省_列表", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData listProvince(SysAreaParam sysAreaParam) {
        return new SuccessResponseData(sysAreaService.listProvince());
    }

    /**
     * 系统区域列表（市）
     *
     * @author xuyuxiang
     * @date 2020/3/20 21:23
     */
    @GetMapping("/business/sysArea/listCity")
    @BusinessLog(title = "市_列表", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData listCity(SysAreaParam sysAreaParam) {
        return new SuccessResponseData(sysAreaService.listCity(sysAreaParam));
    }

    /**
     * 系统区域列表（区/县）
     *
     * @author xuyuxiang
     * @date 2020/3/20 21:23
     */
    @GetMapping("/business/sysArea/listRegion")
    @BusinessLog(title = "区/县_列表", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData listRegion(SysAreaParam sysAreaParam) {
        return new SuccessResponseData(sysAreaService.listRegion(sysAreaParam));
    }

    @PostMapping("/business/sysArea/selectCity")
    public ResponseData selectCity(SysAreaParam sysAreaParam){
        List<SysArea> provinceList = new ArrayList<>();
        List<SysArea> cityList = new ArrayList<>();
        {
            LambdaQueryWrapper<SysArea> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysArea::getLevelCode, 0);
            provinceList = sysAreaService.list(wrapper);
        }
        {
            LambdaQueryWrapper<SysArea> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysArea::getLevelCode, 1);
            List<SysArea> sysAreas = sysAreaService.list(wrapper);
            for (SysArea s : sysAreas) {
                if(s.getName().contains("县")){
                    continue;
                }
                if(s.getName().contains("区")){
                    s.setName(s.getShortName());
                }
                if (s.getName().equals("北京")||s.getName().equals("天津")
                        ||s.getName().equals("上海")||s.getName().equals("重庆")){
                    s.setName(s.getName() +"市");
                }
                cityList.add(s);
            }
        }
        JSONObject result = new JSONObject();
        HashMap<String, List<JSONObject>> provinceMap = new HashMap<>();
        //按照abcd分组
        for (SysArea s : provinceList) {
            String c =  s.getPinyin().substring(0,1);
            List<JSONObject> provinces = provinceMap.get(c);
            if (provinces == null) {
                provinces = new ArrayList<>();
            }
            JSONObject element = new JSONObject();
            element.put("name",s.getName());
            JSONArray cities = new JSONArray();
            for (SysArea a : cityList) {
                if (a.getParentCode().equals(s.getAreaCode())) {
                    cities.add(a.getName());
                }
            }
            element.put("city",cities);
            provinces.add(element);
            provinceMap.put(c,provinces);
        }
        JSONObject city = new JSONObject();
        HashMap<String, List<String>> cityMap = new HashMap<>();
        for (SysArea s : cityList) {
            String c =  s.getPinyin().substring(0,1);
            List<String> cities = cityMap.get(c);
            if (cities == null) {
                cities = new ArrayList<>();
            }
            cities.add(s.getName());
            cityMap.put(c,cities);
        }
        result.put("province", provinceMap);
        result.put("city", cityMap);
        return new SuccessResponseData(result);
    }
}
